package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import models.User;
import models.Course;

public class MyCoursesController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        LinkedList<Course> mycourses = new LinkedList<Course>();
        Course tempcourse = new Course();
        HttpSession sessie = request.getSession();
        User user = (User) sessie.getAttribute("gebruikerinfo");

        LinkedList<Course> allcourses = new LinkedList<Course>(queryManager.getAllCourses());
        LinkedList<Integer> subscribedcourses = new LinkedList<Integer>(queryManager.getallsubscribedcoursesbyid(user.getID()));

        for (int i = 0; i < allcourses.size(); i++) {
            tempcourse = allcourses.get(i);

            for (int y = 0; y < subscribedcourses.size(); y++) {
                if (tempcourse.getCourseID() == subscribedcourses.get(y)) {
                    mycourses.add(tempcourse);
                }
            }
        }

        request.setAttribute("myCoursesUitSessie", mycourses);

        dbManager.closeConnection();

        RequestDispatcher dispatcher = request.getRequestDispatcher("/my_courses.jsp");
        dispatcher.forward(request, response);
    }
}
